home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / intuition / NewMenu.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  7.4 KB  |  282 lines

  1. " ---------------------------------------------------------------------
  2. * MenuStrip class is an Array of NewMenu Objects.  It also contains the
  3. * methods to set & clear itself from a parent Window.
  4. * --------------------------------------------------------------------- 
  5. "
  6. Class MenuStrip :Object ! mStrip parentWindow menuSize !
  7. [
  8.    attachedTo
  9.  
  10.      ^ parentWindow
  11. |
  12.    attachTo: aWindow ! check !
  13.  
  14.      parentWindow <- aWindow windowObject.
  15.  
  16.      " This all-in-one primitive does CreateMenus(), LayoutMenus() &
  17.      * SetMenuStrip():
  18.      "
  19.      check <- <primitive 181 21 mStrip parentWindow>.
  20.      
  21.      (check ~= true)
  22.         ifTrue: [ 'Menu Strip NOT Attached!!' print.
  23.                    parentWindow <- nil ]
  24. |
  25.    hide
  26.  
  27.      <primitive 181 22 parentWindow>
  28. |
  29.    append: newMenuObject
  30.  
  31.      (1 to: menuSize) 
  32.         do: [ :i | ((mStrip at: i) isNil)
  33.                      ifTrue: [mStrip at: i put: newMenuObject. 
  34.                                                  
  35.                               ^ self ] ].
  36.      ^ self
  37. |
  38.    new: numberOfElements  " Must include space for NM_END as well. "
  39.  
  40.      menuSize <- numberOfElements.
  41.      
  42.      mStrip   <- Array new: numberOfElements.
  43.      
  44.      ^ self
  45. ]
  46.  
  47. " ---------------------------------------------------------------------
  48. * NewMenu Class is the class that creates NewMenu items that are
  49. * used by the gadtools.library
  50. *
  51. * structureArray is an Array Object with the following
  52. * elements in the given order:
  53. * ele[1] <- nm_Type,          ele[2] <- nm_Label,
  54. * ele[3] <- nm_CommKey,       ele[4] <- nm_Flags,
  55. * ele[5] <- nm_MutualExclude, ele[6] <- nm_UserData 
  56. *
  57. * ele[6] is an Array as follows:
  58. *
  59. *    udele[1] <- Menu Type (#NM_TITLE, #NM_ITEM, #NM_SUB, #IM_ITEM,
  60. *                           or #IM_SUB)
  61. *    udele[2] <- menu ID_String,
  62. *    udele[3] <- userData (Usually a #methodSymbol,
  63. *    udele[4] <- equivalent to ele[3] (nm_CommKey)
  64. *
  65. *   Making a menu:
  66. *
  67. *   menuStrip  <- MenuStrip new: 3
  68. *   menu1      <- NewMenu new
  69. *   menu1Array <- menu1 titleMenuArray: 'PROJECT'
  70. *   menu2      <- NewMenu new
  71. *
  72. *   menuAction <- Array new: 3
  73. *   menuAction at: 1 put: #loadFileActionMethod
  74. *   menuAction at: 2 put: 'Load a file...'
  75. *   menuAction at: 3 put: 'L'
  76. *
  77. *   menu2Array <- menu2 menuItemArray: 'Load a file...' key: 'L' 
  78. *                               flags: 0 data: menuAction
  79. *
  80. *   menu3      <- NewMenu new
  81. *
  82. *   You MUST have one of these for a valid menu strip:
  83. *   menu3 fillNewMenuItemWith: (menu3 endOfMenuArray)
  84. *
  85. *   menuStrip at: 1 put: menu1
  86. *   menuStrip at: 2 put: menu2
  87. *   menuStrip at: 3 put: menu3
  88. *
  89. *   menuStrip attachTo: myWindow
  90. * --------------------------------------------------------------------- 
  91. Class NewMenu :Object
  92. ! private intuition windowObj structArray myAction !
  93. [
  94.    xxxDisposeMenu
  95.  
  96.       <primitive 239 1 0 (self xxxMenu)>.
  97.       
  98.       <primitive 250 5 0 private>.
  99. |
  100.    dispose               " Synonym for xxxDisposeMenu "
  101.  
  102.       self xxxDisposeMenu.
  103.       
  104.       ^ nil
  105. |
  106.    new: newAction
  107.  
  108.       intuition   <- Intuition new.   
  109.       structArray <- Array     new: 6.
  110.       private     <- <primitive 239 1 1 1>.
  111.  
  112.       self value: newAction.
  113.       
  114.       ^ self
  115. |
  116.    addedTo: menuStrip
  117.  
  118.       ^ menuStrip append: (self xxxMenu).
  119. |
  120.    registerTo: myWindowObject
  121.    
  122.       (myWindowObject isNil)
  123.          ifTrue: [ 'NewMenu Object given a nil Window object!' print.
  124.                    ^ nil
  125.                  ].
  126.  
  127.       ^ windowObj <- myWindowObject
  128. |
  129.    xxxMenu
  130.  
  131.       " In order to avoid changing the underlying primitive code
  132.       * (again!), this method will return the NewMenu Object
  133.       * from the private Object:
  134.       "
  135.       ^ (private at: 1)
  136. |
  137.    value: newAction       " testMenu is an example Action "
  138.       
  139.       ^ myAction <- newAction
  140. |
  141.    value
  142.       
  143.       ^ myAction
  144. |
  145.    initializeControl  " Only NewGadgets need this method to do something: "
  146.    
  147.       ^ nil
  148. |
  149.    testMenu ! ans !  
  150.    
  151.       " this is a test method for UserGUI to use & is setup
  152.       * by setting >> userData at: 3 put: #testMenu << 
  153.       "
  154.       amigatalk setIOTitle: 'Menu Test Action:'.
  155.       
  156.       ans <- amigatalk getUserResponse: ('You selected: "', 
  157.                                          ((structArray at: 2) asString), 
  158.                                          '" Do you want to continue?').
  159.       
  160.       (ans = 0)
  161.          ifTrue:  [ ^ true  ]
  162.          ifFalse: [ ^ false ] " Terminate UserGUI/controlLoop "
  163. |
  164.    menuUserData: userDataArray
  165.    
  166.       structArray at: 6 put: userDataArray
  167. |
  168.    fillNewMenuItemWith: structureArray
  169.    
  170.       (1 to: 6) do: [ :i | structArray at: i 
  171.                                       put: (structureArray at: i) ].
  172.       
  173.       (<primitive 239 1 2 1 structArray private> ~= true)
  174.          ifTrue: [ self xxxDisposeMenu.
  175.                    'ERROR:  Could NOT fill a NewMenu entry!' print.
  176.                    ^ nil
  177.                  ] 
  178. |
  179.    endOfMenuArray ! endArray !  " Required by ALL menuStrips "
  180.  
  181.       endArray <- self xxxMakeArray: nil k: nil f: 0 data: 0.
  182.       
  183.       endArray at: 1 put: (intuition systemTag: #NM_END).
  184.  
  185.       ^ endArray
  186. |
  187.    titleMenuArray: title ! rval ! " Make a new Menu TITLE: "
  188.  
  189.       rval <- self xxxMakeArray: title k: nil f: 0 data: nil.
  190.  
  191.       rval at: 1 put: (intuition systemTag: #NM_TITLE).
  192.  
  193.       self fillNewMenuItemWith: rval.
  194.       
  195.       ^ rval
  196. |
  197.    barLabel ! rval ! " Make a new Menu item NM_BARLABEL: "
  198.  
  199.       rval <- self xxxMakeArray: nil k: nil f: 0 data: nil.
  200.  
  201.       rval at: 1 put: (intuition systemTag: #NM_BARLABEL).
  202.       
  203.       self fillNewMenuItemWith: rval.
  204.  
  205.       ^ rval
  206. |
  207.    menuItemArray: title key: commKey flags: flags data: userData ! rval !    
  208.    
  209.       " Make a new Menu ITEM: "
  210.  
  211.       rval <- self xxxMakeArray: title    k: commKey 
  212.                               f: flags data: userData.
  213.  
  214.       rval at: 1 put: (intuition systemTag: #NM_ITEM).
  215.  
  216.       self fillNewMenuItemWith: rval.
  217.             
  218.       ^ rval
  219. |
  220.    subItemArray: title key: commKey flags: flags data: userData ! rval !
  221.  
  222.       " Make a new menu SUB: "
  223.  
  224.       rval <- self xxxMakeArray: title    k: commKey 
  225.                               f: flags data: userData.
  226.  
  227.       rval at: 1 put: (intuition systemTag: #NM_SUB).
  228.       
  229.       self fillNewMenuItemWith: rval.
  230.  
  231.       ^ rval
  232. |
  233.    menuItemSpace ! rval !    
  234.    
  235.       " Make a new Menu ITEM that is blank #NM_IGNORE: "
  236.  
  237.       rval <- self xxxMakeArray: nil k: nil f: 0 data: nil.
  238.  
  239.       rval at: 1 put: (intuition systemTag: #NM_IGNORE).
  240.       
  241.       self fillNewMenuItemWith: rval.
  242.  
  243.       ^ rval
  244. |
  245.    menuImageArray: image key: commKey flags: flags data: userData ! rval !
  246.  
  247.       " Make a new Menu IM_ITEM: "
  248.  
  249.       rval <- self xxxMakeArray: image    k: commKey 
  250.                               f: flags data: userData.
  251.  
  252.       rval at: 1 put: (intuition systemTag: #IM_ITEM).
  253.       
  254.       self fillNewMenuItemWith: rval.
  255.  
  256.       ^ rval
  257. |
  258.    subImageArray: image key: commKey flags: flags data: userData ! rval !
  259.  
  260.       " Make a new Menu IM_SUB: "
  261.  
  262.       rval <- self xxxMakeArray: image    k: commKey 
  263.                               f: flags data: userData.
  264.  
  265.       rval at: 1 put: (intuition systemTag: #IM_SUB).
  266.  
  267.       self fillNewMenuItemWith: rval.
  268.       
  269.       ^ rval
  270. |
  271.    xxxMakeArray: t k: k f: f data: data
  272.  
  273.       structArray at: 2 put: t.     " menu title string   "
  274.       structArray at: 3 put: k.     " menu shortcut key   "
  275.       structArray at: 4 put: f.     " menu flags          "
  276.       structArray at: 5 put: 0.     " mutualexclude value "
  277.       structArray at: 6 put: data.  " menu userdata       "
  278.       
  279.       ^ structArray
  280. ]
  281.